<?php
namespace app\admin\controller;

use app\common\builder\ZBuilder;
use app\admin\model\User as UserModel;
use think\Image;
use think\File;
use think\facade\Hook;
use think\Db;
use think\facade\Env;

class User extends Admin
{
    public function index()
    {
        // 查询
        $map = $this->getMap();
        // 数据列表
        $userModel = new UserModel();
        $data_list = $userModel->getList($map);
        foreach($data_list as $k=>$row){
            $data_list[$k]['headimgurl'] = getImgUrl($row['headimgurl']);
        }
        return ZBuilder::make('table')
            ->setSearch(['q.title' => '标题','w.title' => '所属小程序']) // 设置搜索框
            ->setPageTitle('用户列表')
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['nickname', '用户昵称'],
                ['wtitle', '所属小程序'],
                ['headimgurl', '头像',parse_attr(Db::name('admin_config')->where('name', 'upload_driver')->value('options'))],
                ['province', '地区'],
                ['addtime', '添加时间', 'datetime'],
                ['last_login_time', '最后登录时间', 'datetime'],
                ['status', '状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
            ->addRightButtons('delete') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染模板
    }

    public function delete($record = [])
    {
        $data = $this->request->param();
        $ids = $data['ids'];
        if($ids){
            if(is_array($ids)){
                foreach($ids as $id){
                    $wxapp_id = Db::name('wx_user')->where('id',$id)->value('wxapp_id');
                    Db::name('wxapp_log')->where('wxapp',$wxapp_id)->setDec('total_user',1);
                    Db::name('wx_user')->delete($id);
                }
            }else{
                $wxapp_id = Db::name('wx_user')->where('id',$id)->value('wxapp_id');
                Db::name('wxapp_log')->where('wxapp',$wxapp_id)->setDec('total_user',1);
                Db::name('wx_user')->delete($ids);
            }
        }
        $this->success('删除成功');
    }
}